package com.hua.ui;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import javax.swing.JSplitPane;
import javax.swing.JSlider;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.sound.midi.Soundbank;
import javax.swing.Box;
import javax.swing.JSeparator;
import javax.swing.ImageIcon;
import javax.swing.SwingConstants;
import javax.swing.UIManager;

import com.formdev.flatlaf.FlatLightLaf;
import com.hua.Dao.SqlService;
import com.hua.Model.PermissInfo;
import com.hua.Model.UserInfo;
import com.hua.Service.funtion;

import java.awt.Image;
import java.sql.SQLException;
import java.util.List;
import javax.swing.JCheckBox;

public class login extends JPanel {
	private JTextField login_account;
	private JPasswordField login_passwordField;
	private int width = 300, height = 300;
	private ImageIcon image;

	static {
		// 先设置外观，再创建UI。
		// 为了保证创建UI时，已经实现设置好新外观，
		// 最好把设置外观的代码写在静态块中，
		// 并且把这个静态块写在类定义的最前面。
		FlatLightLaf.setup();
		// 主题的切换
		try {
			UIManager.setLookAndFeel(new FlatLightLaf());
		} catch (Exception ex) {
			System.err.println("Failed to initialize LaF");
		}
	}

	/**
	 * Create the panel.
	 */
	public static void check_login(String account, String password) {
		UI display = new UI();
		if (account.isEmpty() || password.isEmpty() || !funtion.check_str(account, "账户") || funtion.check_str(account, "含中文") || account.length() >30 || account.length() <11 || password.length() < 6) {
			System.out.println(account + "," + password);
			display.tips("提示", "账户名或密码输入异常");
			// System.out.println("账户名或密码不完整");
		} else {
			// 传入加密SHA256函数加密
			funtion fun = new funtion();
			String en_password = fun.encryptSHA256(password);
			// 登录逻辑
			SqlService Service = new SqlService();
			try {
				// 调用登录函数
				List<UserInfo> user = Service.log_in(account, en_password);
				// 抛出结果
				if (user.isEmpty()) {
					display.tips("登录失败", "用户名或者密码错误(x_x)");
					System.out.println(en_password);
				} else {
					// 获取用户的ID
					UserInfo user_info = new UserInfo();
					user_info.setId(user.get(0).getId());
					user_info.setAccount(user.get(0).getAccount());
					user_info.setImg(user.get(0).getImg());
					user_info.setName(user.get(0).getName());
					user_info.setSign(user.get(0).getSign());
					user_info.setPassword(user.get(0).getPassword());
					user_info.setKey(user.get(0).getKey());
					user_info.setStorage(user.get(0).getStorage());
					// 实例权限
					PermissInfo user_permiss = Service.GetPermiss(user.get(0).getId());
					if (user_permiss.getLogin() == 1) {
						// 权限正常
						// 销毁当前窗口
						display.off(user_info, user_permiss);
					} else {
						funtion.tips("登录权限", "你已被禁止登录!想想是不是干了什么谋财害命的事情吧!");
					}
				}
			} catch (SQLException e1) {
				// 抛出异常
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	public login() {
		setLayout(null);

		JLabel lblNewLabel = new JLabel("欢迎登陆");
		lblNewLabel.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 22));
		lblNewLabel.setBounds(497, 13, 113, 69);
		add(lblNewLabel);

		login_account = new JTextField();
		login_account.setBounds(443, 116, 253, 33);
		add(login_account);
		login_account.setColumns(10);

		JButton btnNewButton = new JButton("登陆");

		btnNewButton.setBounds(569, 339, 160, 53);
		add(btnNewButton);

		JLabel lblNewLabel_1 = new JLabel("账户:");
		lblNewLabel_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 22));
		lblNewLabel_1.setBounds(344, 116, 97, 33);
		add(lblNewLabel_1);

		JLabel lblNewLabel_1_1 = new JLabel("密码:");
		lblNewLabel_1_1.setFont(new Font("Alimama ShuHeiTi", Font.BOLD, 22));
		lblNewLabel_1_1.setBounds(344, 187, 97, 33);
		add(lblNewLabel_1_1);

		login_passwordField = new JPasswordField();
		login_passwordField.setBounds(443, 189, 253, 31);
		add(login_passwordField);

		JButton btnNewButton_1 = new JButton("游客登录");
		btnNewButton_1.setBounds(381, 339, 160, 53);
		add(btnNewButton_1);

		JLabel lblNewLabel_2 = new JLabel("");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.LEFT);
		image = new ImageIcon("src/main/resources/images/webdisk.png");
		// image.setImage(image.getImage().getScaledInstance(width,
		// height,Image.SCALE_DEFAULT));
		Image img = image.getImage();
		img = img.getScaledInstance(width, height, Image.SCALE_DEFAULT);
		image.setImage(img);
		lblNewLabel_2.setIcon(image);
		lblNewLabel_2.setBounds(14, 62, 316, 303);
		add(lblNewLabel_2);

		JCheckBox chckbxNewCheckBox = new JCheckBox("登陆即代表同意《数据窃取协议》");
		chckbxNewCheckBox.setFont(new Font("Alimama ShuHeiTi", Font.PLAIN, 15));
		chckbxNewCheckBox.setSelected(true);
		chckbxNewCheckBox.setBounds(344, 275, 352, 27);
		add(chckbxNewCheckBox);

		// 事件处理
		// 添加点击事件监听器
		btnNewButton.addActionListener(e -> {
			if (chckbxNewCheckBox.isSelected()) {
				check_login(login_account.getText(), new String(login_passwordField.getPassword()));
			} else {
				funtion.wanr_tips("协议同意", "请勾选同意协议");
			}
		});
		// 密码框回车监听
		login_passwordField.addActionListener(e -> {
			if (chckbxNewCheckBox.isSelected()) {
				check_login(login_account.getText(), new String(login_passwordField.getPassword()));
			} else {
				funtion.wanr_tips("协议同意", "请勾选同意协议");
			}
		});
		// 游客登录
		// 账户回车监听，使用游客登录
		login_account.addActionListener(e -> {
			if (chckbxNewCheckBox.isSelected()) {
				UI display = new UI();
				// 默认值传递，方便测试
				display.off(
						new UserInfo(1, "游客", "11111111111",
								"bcb15f821479b4d5772bd0ca866c00ad5f926e3580720659cc80d39c9d09802a", "落霞与孤鹜齐飞，秋水共长天一色",
								"https://pic.imgdb.cn/item/654dbe5ac458853aef3328a9.png", "open", 99999),
						new PermissInfo(1, 1, 0, 0, 1, 0, 1, 1, 1));
			} else {
				funtion.wanr_tips("协议同意", "请勾选同意协议");
			}
		});
		// 点击游客
		btnNewButton_1.addActionListener(e -> {
			if (chckbxNewCheckBox.isSelected()) {
				UI display = new UI();
				// 默认值传递，方便测试
				display.off(
						new UserInfo(1, "游客", "11111111111",
								"bcb15f821479b4d5772bd0ca866c00ad5f926e3580720659cc80d39c9d09802a", "落霞与孤鹜齐飞，秋水共长天一色",
								"https://pic.imgdb.cn/item/654dbe5ac458853aef3328a9.png", "open", 99999),
						new PermissInfo(1, 1, 0, 0, 1, 0, 1, 1, 1));
			} else {
				funtion.wanr_tips("协议同意", "请勾选同意协议");
			}
		});
		UIManager.put("TextComponent.arc", 5);
		//
	}
}